package com.example.leetdemo.finash;

import java.util.ArrayList;
import java.util.List;

public class Solution938 {

    public int rangeSumBST(TreeNode root, int low, int high) {
        List<TreeNode> list = new ArrayList<>();
        list.add(root);
        int i = 0;
        while (list.size() > 0) {
            List<TreeNode> temp = new ArrayList<>();
            for (TreeNode node : list) {
                if (node.left != null) {
                    temp.add(node.left);
                }
                if (node.right != null) {
                    temp.add(node.right);
                }
                if (node.val >= low && node.val <= high) {
                    i += node.val;
                }
            }
            list = temp;
        }
        return i;
    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
